const baseURL = 'https://www.xiongmaoyouxuan.com/api'

const request = ({url, method = 'GET', data, header = {}} = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + url,
      method,
      data,
      timeout: 5000,
      header: {
        ...header,
        'token': 'asdlfjkasdlfkjasdklfjalsdkjflasdjflkasdjjk'
      },
      success: res => {
        if (res.statusCode === 200) {
          const {code, data} = res.data
          if (code === 200) {
            resolve(data)
            return
          }
        }
        reject(new Error('请求资源有误：' + res.data.code))
      },
      fail: err => {
        console.log('请求失败：', err)
        reject(err)
      }
    })
  })
}

const get = url => request({url, method: 'GET'})

const post = (url, data) => request({url, method: 'POST', data})

module.exports = {
  request,
  get,
  post
}

// exports.request = request
// exports.get = get
// exports.post = post

